package ema.tpjee.banking.dao.local;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import ema.tpjee.banking.dao.BankDao;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.City;
import ema.tpjee.utils.local.RandomString;

public class BankDaoImpl implements BankDao, Serializable {


	private static final long serialVersionUID = -5443611226894567253L;
	private List<Bank> list = new ArrayList<Bank>();
	private int id=1000;
	

	public BankDaoImpl() {
		super();
		System.out.println("creating BankDaoImpl");
	}

	public Bank add(Bank b) {
		list.add(b);
		return b;
	}

	public List<Bank> populate() {
			
		Bank b = create();
		b.setName("Crédit Arboricole");
		b.setPhone("123456");
		b.setAddress( "3, rue des sapins");
		b.setZipCode("75000");
		
		b = create();
		b.setName("Internet Bank");
		b.setPhone("7890123");
		b.setAddress( "10, avenue de la Californie");
		b.setZipCode("13000");
	

		b = create();
		b.setName("Société Géniale");
		b.setPhone("45698741");
		b.setAddress( "1, parvis de la Défonse");
		b.setZipCode("92400");


		for (int i = 0; i < 10; i++) {
			b = createRandom();
		}
		return list;
	}



	public void setUnitTestResult(String unitTestResult) {
		// this.unitTestResult = unitTestResult;
	}

	public Collection<Bank> reverseList() {
		List<Bank> rev = new ArrayList<Bank>();
		for (Iterator<Bank> i = list.iterator(); i.hasNext();) {
			Bank b = i.next();
			rev.add(b);
		}
		Collections.reverse(rev);
		return rev;
	}

	public Bank find(int id) {
		for (Iterator<Bank> i = list.iterator(); i.hasNext();) {
			Bank b = i.next();
			if (b.getId() == id)
				return b;
		}
		return null;
	}

	/**
	 * after deletion returns the previous if exists else the next if exists
	 * else null
	 */
	public Bank delete(Bank b) {
		Bank result = Paging.prior(list, b);
		if (result == null)
			result = Paging.next(list, b);
		list.remove(b);
		return result;
	}

	public Bank first() {
		if (list.size()>0)
	  	  return list.iterator().next();
		else
		  return null;
	}

	public Bank last() {
		return list.get(list.size() - 1);
	}

	private Bank copyBank(Bank b) {
		Bank newb = new Bank();
		//newb.setId(id++);
		if (b.getAddress() != null)
			newb.setAddress(b.getAddress());
		if (b.getName() != null)
			newb.setName(b.getName());
		if (b.getPhone() != null)
			newb.setPhone(b.getPhone());
		// if (b.getZipCode() != 0)
		newb.setZipCode(b.getZipCode());
		return newb;
	}

	public Bank cloneBank(Bank b) {
		Bank cloned = copyBank(b);
		list.add(cloned);
		return cloned;
	}

	public Bank foobar(Bank b) {
		// if (b.getZipCode()>150000)
		// throw new RuntimeException("foobar is too big!");
		return b;
	}

	public Bank save(Bank b) {
		Bank bb = find(b.getId());
		// if (bb != null)
		// bb = copyBank(b);
		if (bb != null) {
			if (b.getAddress() != null)
				bb.setAddress(b.getAddress());
			if (b.getName() != null)
				bb.setName(b.getName());
			if (b.getPhone() != null)
				bb.setPhone(b.getPhone());
			// if (b.getZipCode() != 0)
			bb.setZipCode(b.getZipCode());
		} else
			throw new RuntimeException("la modification de " + b + " a �chou�");
		return bb;
	}

	public Bank next(Bank b) {
		return Paging.next(list, b);
	}

	public Bank prior(Bank b) {
		return Paging.prior(list, b);
	}

	public Bank create() {
		Bank b = new Bank();
		b.setId(id++);
		b.setName("nouvelle banque " + b.getId());
		list.add(b);
		return b;
	}

	public boolean isEmpty() {
		return list.size() == 0;
	}

	public Bank clone(Bank b) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Bank> getList() {
		return list;
	}

	public void setList(List<Bank> banks) {
		// this.banks = banks;
	}

	public java.util.List getItems() {
		return list;
	}

	public void setItems(java.util.List items) {
		// this.list = items;
	}

	public Bank merge(Bank b) {
		return save(b);
	}

	static Random rand = new Random();
	
	public Bank createRandom() {
		Bank b = create();
		b.setName("Banque " + RandomString.generateStringAlpha(10));
		b.setAddress(rand.nextInt(100) + " rue "
				+ RandomString.generateStringAlpha(10));
		b.setPhone(rand.nextInt(100) + "." + rand.nextInt(100) + "."
				+ rand.nextInt(100) + "." + rand.nextInt(100) + "."
				+ rand.nextInt(100));
		b.setZipCode(Integer.toString(30000 + rand.nextInt(1000)));
		return b;
	}

	public Boolean isSame(Bank a, Bank b) {
		return a!=null && b!=null && a.getId() == b.getId();
	}

}
